package com.anyonavinfo.cpaddemo.data.timeinfo.source;


import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.format.DateFormat;
import android.text.style.RelativeSizeSpan;

import com.anyonavinfo.cpaddemo.Application;
import com.anyonavinfo.cpaddemo.data.timeinfo.TimeInfo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/2/1.
 */

public class TimeCalculator {

    private String[] mWeeks = new String[] { "周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    private static TimeCalculator INSTANCE = null;
    private Context mContext;


    public static TimeCalculator getInstance(){
        if (INSTANCE == null) {
            INSTANCE = new TimeCalculator();

        }
        return INSTANCE;
    }

    private TimeCalculator(){
        mContext = Application.getContextObject();
    }

    public TimeInfo calCurTime(){
        TimeInfo timeInfo = new TimeInfo();

        long ld = System.currentTimeMillis();
        Date curDate = new Date(ld);

        SimpleDateFormat formatter;

        Calendar calendar= Calendar.getInstance();
        calendar.setTime(curDate);

        int month,day,week;

        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        week = calendar.get(Calendar.DAY_OF_WEEK);


        String time;
        //2016/10/11 By ChenZui
        if(DateFormat.is24HourFormat(mContext)){
            formatter = new SimpleDateFormat("HH:mm");
            time = formatter.format(curDate);
            time = time.split(":")[0]+" : "+time.split(":")[1];
            timeInfo.setTime(time);
        }else{
            formatter = new SimpleDateFormat("aa h:mm");
            time = formatter.format(curDate);
            time = time.split(":")[0]+" : "+time.split(":")[1];
            Spannable WordtoSpan = new SpannableString(time);
            WordtoSpan.setSpan(new RelativeSizeSpan((float) 0.33), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            WordtoSpan.setSpan(new RelativeSizeSpan((float) 0.75), 2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            timeInfo.setTime(time);
        }

        timeInfo.setDate(month + "/" + day + "     " + mWeeks[week - 1]);
        return timeInfo;
    }


}
